var draw = {};

draw.price = 1000;
draw.degree = {};
draw.keyword = null;
draw.totalPrice = 0;
draw.drawId = 0;
draw.ids = ['name', 'request'];

draw.categoryInit = function() {
	
}

draw.getData = function() {
	var param = common.GetParameters();
	if (param.hasOwnProperty("type") && param.type == "update") {
		var id = param.id;
		var req = new common.Request();
		var reqData = {};
		reqData['id'] = id;
		reqData['flag'] = 2;
		var url = "orderInfo/findInfoByFlag.action";
		req.sendMethodRequest("get", url, reqData, function(e){
			if (e != null && e.isSuccess) {
				if (e.info.hasOwnProperty('degree')) {
					var degree = e.info.degree;
					if (degree != null && degree.indexOf(",")) {
						var degreeArray = degree.split(",");
						var category_info = $('.category_info');
						for (var i = 0; i < category_info.length; i ++) {
							for (var j = 0; j < degreeArray.length; j ++) {
								if (degreeArray[j] == $(category_info[i]).val()) {
									$(category_info[i]).attr('checked','checked');
									write.degree[degreeArray[j]] = degreeArray[j];
								}
							}
						}
					}
				}
				write.writeId = e.info.id;
				if (e.info.keyword != null) {
					$('#company_type').val(e.info.keyword);
				}
				$('#name').val(e.info.name);
				$('#total_price').html(e.info.price);
				$('#request').val(e.info.descQues);
			}
		}, function(e){
			
		})
	}
}

draw.getHeight = function () {
    var height = $(document).height();
    window.parent.index.iframeResize(height);
}

draw.upload = function(){
	var data = {};
	$.ajaxFileUpload({
		data: data,
		type: 'post',
        url: "drawInfo/fileUpload.action", // 你处理上传文件的服务端
        secureuri: false,
        fileElementId: 'file_image', // 图片标签id
        dataType: 'json',
        success: function (result) {
        	console.log(result);
        },error: function (result){
        	
        }
	});
}

draw.onchange = function(obj) {
	if (obj.files && obj.files[0]) {
		$('#desction').html(obj.files[0].name);
	}
}

draw.fileDialog = function() {
	$('#file_image').remove();
	var inputObj = document.getElementById("file_image");
	inputObj = null;
	if (inputObj == null) {
		inputObj = document.createElement('input');
		inputObj.setAttribute("accept", ".jpg,.jpeg,.gif,.png,.bmp,.JPG,.JPEG,.GIF,.PNG,.BMP")
		inputObj.setAttribute('id','file_image');
		inputObj.setAttribute('type','file');
		inputObj.setAttribute("style",'visibility:hidden');
		inputObj.setAttribute("onchange", 'draw.onchange(this)');
		inputObj.setAttribute("class", "Upload_file");
		inputObj.setAttribute("name", "file_image");
		document.getElementById("yanzheng").appendChild(inputObj);
		inputObj.click();
	} else {
		inputObj.click();
	}
}

draw.submit = function () {
	$('#submit_button').click(function(){
		var sendSubmit = function() {
			var param = common.GetParameters();
			var req = new common.Request();
			var reqData = {};
			var keywordStr = "";
			for (var key in write.degree) {
				keywordStr = keywordStr + key + ",";
			}
			if (keywordStr.indexOf(",") > -1) {
				keywordStr = keywordStr.substring(0, keywordStr.length - 1);
			}
			var url = "drawInfo/addFastDrawInfo.action";
			if (param != null && param.hasOwnProperty("type") && param.type == "update") {
				url = "drawInfo/updateFastDrawInfo.action";
				reqData["id"] = write.writeId;
			}
			reqData['degree'] = keywordStr;
			reqData['keyword'] = $('#company_type').val();
			reqData['name'] = $('#name').val();
			reqData['price'] = $('#total_price').html();
			reqData['request'] = $('#request').val();
			req.sendRequest(url, reqData, function(e){
				if (e != null && e.isSuccess != null && e.isSuccess) {
					window.location = "order_success.html?type=write";
				} else {
					
				}
			}, function(e){
				
			});
		}
		var ver = new verify.login(draw.ids, 'empty_style', function() {
			new sendSubmit();
		}, function() {
			
		});
	});
}


draw.init = function() {
	try {
		draw.getHeight();
	} catch (e){
		
	}
	draw.getData();
	draw.submit();
	draw.categoryInit();
	draw.fileDialog();
	$('#code_button').click(function(){
		draw.fileDialog();
	});
	$('#submit_button').click(function(){
		draw.upload();
	});
}

$(document).ready(function() {
	draw.init();
});